;; add package about org
;; (add-to-list 'damacs/damacs-packages '(org-plus-contrib) t)
;; (add-to-list 'damacs/damacs-packages '(org-ref org-ref) t)
;; (add-to-list 'damacs/damacs-packages '(zotxt zotxt) t)
;; (add-to-list 'damacs/damacs-packages '(ob-ipython ob-ipython) t)
;; (add-to-list 'damacs/damacs-packages '(epresent epresent) t)
;; (add-to-list 'damacs/damacs-packages '(org-bullets org-bullets) t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; babel setting
;; From: https://pank.eu/blog/pretty-babel-src-blocks.html
(with-eval-after-load 'org
  (defvar-local rasmus/org-at-src-begin -1
    "Variable that holds whether last position was a ")

  (defvar rasmus/ob-header-symbol ?☰
    "Symbol used for babel headers")

  (defun rasmus/org-prettify-src--update ()
    (let ((case-fold-search t)
          (re "^[ \t]*#\\+begin_src[ \t]+[^ \f\t\n\r\v]+[ \t]*")
          found)
      (save-excursion
        (goto-char (point-min))
        (while (re-search-forward re nil t)
          (goto-char (match-end 0))
          (let ((args (org-trim
                       (buffer-substring-no-properties (point)
                                                       (line-end-position)))))
            (when (org-string-nw-p args)
              (let ((new-cell (cons args rasmus/ob-header-symbol)))
                (cl-pushnew new-cell prettify-symbols-alist :test #'equal)
                (cl-pushnew new-cell found :test #'equal)))))
        (setq prettify-symbols-alist
              (cl-set-difference prettify-symbols-alist
                                 (cl-set-difference
                                  (cl-remove-if-not
                                   (lambda (elm)
                                     (eq (cdr elm) rasmus/ob-header-symbol))
                                   prettify-symbols-alist)
                                  found :test #'equal)))
        ;; Clean up old font-lock-keywords.
        (font-lock-remove-keywords nil prettify-symbols--keywords)
        (setq prettify-symbols--keywords (prettify-symbols--make-keywords))
        (font-lock-add-keywords nil prettify-symbols--keywords)
        (while (re-search-forward re nil t)
          (font-lock-flush (line-beginning-position) (line-end-position))))))

  (defun rasmus/org-prettify-src ()
    "Hide src options via `prettify-symbols-mode'.

  `prettify-symbols-mode' is used because it has uncollpasing. It's
  may not be efficient."
    (let* ((case-fold-search t)
           (at-src-block (save-excursion
                           (beginning-of-line)
                           (looking-at "^[ \t]*#\\+begin_src[ \t]+[^ \f\t\n\r\v]+[ \t]*"))))
      ;; Test if we moved out of a block.
      (when (or (and rasmus/org-at-src-begin
                     (not at-src-block))
                ;; File was just opened.
                (eq rasmus/org-at-src-begin -1))
        (rasmus/org-prettify-src--update))
      (setq rasmus/org-at-src-begin at-src-block)))

  (defun rasmus/org-prettify-symbols ()
    (mapc (apply-partially 'add-to-list 'prettify-symbols-alist)
          (cl-reduce 'append
                     (mapcar (lambda (x) (list x (cons (upcase (car x)) (cdr x))))
                             `(("#+begin_src" . ?✎) ;; ✎
                               ("#+end_src"   . ?□) ;; ⏹
                               ("#+header:" . ,rasmus/ob-header-symbol)
                               ("#+begin_quote" . ?»)
                               ("#+end_quote" . ?«)))))
    (turn-on-prettify-symbols-mode)
    (add-hook 'post-command-hook 'rasmus/org-prettify-src t t))
  (add-hook 'org-mode-hook #'rasmus/org-prettify-symbols))


(require 'ob-python)
(org-babel-do-load-languages
 'org-babel-load-languages '((dot . t)
                             (ditaa . t)
                             (lisp . t)
                             (octave . t)
                             (gnuplot . t)
                             (python . t)
                             (C . t)
                             (shell . t)
                             (java . t)
                             (latex . t)
                             (clojure . t)
                             (ruby . t)
                             (plantuml . t)
                             (cypher . t)
                             (emacs-lisp . t))
 )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Set org-bullets
(use-package org-bullets
  :config (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Blog Settings From: https://github.com/Linusp/emacs_config/blob/master/emacs_config.org
;; Create new blog and write
(defvar post-org-dir "~/400dama/blog/_orgs/")
(defun blog-post (title)
  (interactive "sEnter title: ")
  (let ((post-file (concat post-org-dir
                           (format-time-string "%Y-%m-%d")
                           "-"
                           title
                           ".org")))
    (defvar post-html-link (concat "400dama.xyz/"
                                   (format-time-string "%Y/%m/%d")
                                   "/"
                                   title
                                   ".html"))
    (progn
      (switch-to-buffer (find-file-noselect post-file))
      (insert (concat "#+startup: showall\n"
                      "#+options: toc:nil\n"
                      "#+begin_export html\n"
                      "---\n"
                      "layout     : post\n"
                      "title      : \n"
                      "categories : \n"
                      "tags       : \n"
                      "---\n"
                      "#+end_export\n"
                      "#+TOC: headlines 2\n"
                      "\n"
                      "\n"
                      "\n"
                      "#+BEGIN_QUOTE\n"
                      "AUTHOR:  400dama\n\n"
                      "LINK:    https://400dama.xyz/"
                      (format-time-string "%Y/%m/%d")
                      "/"
                      title
                      ".html"
                      "\n\n"
                      "LICENSE: 本博客所有文章除特别声明外，均采用 [[https://creativecommons.org/licenses/by-nc/4.0/][CC BY-NC 4.0]] 许可协议。转载请注明出处！\n"
                      "#+END_QUOTE\n")))
    ))
(defun publish-project (project no-cache)
  (interactive "sName of project: \nsNo-cache?[y/n] ")
  (if (or (string= no-cache "y")
          (string= no-cache "Y"))
      (setq org-publish-use-timestamps-flag nil))
  (org-publish-project project)
  (setq org-publish-use-timestamps-flag t))

;; PUBLISH(org)
(setq org-export-default-language "zh-CN")
(setq org-publish-project-alist
      '(("blog-org"
         ;; Path to your org files.
         :base-directory "~/400dama/blog/_orgs/"
         :base-extension "org"
         ;; Path to your Jekyll project.
         :publishing-directory "~/400dama/blog/_posts/"
         :recursive t
         :htmlized-source t
         :section-numbers nil
         :publishing-function org-html-publish-to-html
         :headline-levels 4
         :html-extension "html"
         :body-only t; Only export section between <body> </body>
         :table-of-contents t         ;test
         )
        ("blog-static"
         :base-directory "~/400dama/blog/_orgs/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php"
         :publishing-directory "~/400dama/blog/_posts/"
         :recursive t
         :publishing-function org-publish-attachment
         )
        ("blog" :components ("blog-org" "blog-static"))))



(provide 'damacs-org)
